<?php


namespace App\Http\Controllers;

use App\Helpers\JwtUtils;
use App\Library\Logger;
use App\Models\UserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\UnauthorizedException;

/**
 * @OA\Info(
 *     version="1.0.0",
 *     title="嘉盛环境 API文档",
 *     description="该文档可供 iOS、Android、web及小程序使用",
 *     @OA\Contact(email="service@josen.info", name="developer", url="https://www.jsjsem.cn/"),
 *     @OA\License(name="private project", url="https://www.jsjsem.cn/")
 * ),
 * @OA\SecurityScheme(securityScheme="bearerAuth", type="http", scheme="bearer")
 */
class BaseController extends \Illuminate\Routing\Controller
{
    protected $logger = null;

    public function __construct()
    {
        // 初始化网络请求配置
        $this->initRequestConfig();

        // 初始化系统常量
        $this->initSystemConst();

        // 设置 Logger
        $controllerName = $this->getCurrentControllerName();
        $this->logger = new Logger('controllers/' . Str::snake($controllerName));

        $userId = JwtUtils::getUserId();
        $user = UserModel::find($userId);
        register('user_id', $userId);
        register('user', $user);
    }

    public function getCurrentControllerName()
    {
        $reflect = new \ReflectionClass($this);
        $shortName = $reflect->getShortName();
        return str_replace('Controller', '', $shortName);
    }

    private function initRequestConfig()
    {
        // 定义是否GET请求
        defined('IS_GET') or define('IS_GET', \request()->isMethod('GET'));

        // 定义是否POST请求
        defined('IS_POST') or define('IS_POST', \request()->isMethod('POST'));

        // 定义是否AJAX请求
        defined('IS_AJAX') or define('IS_AJAX', \request()->ajax());

        // 定义是否PAJAX请求
        defined('IS_PJAX') or define('IS_PJAX', \request()->pjax());

        // 定义是否PUT请求
        defined('IS_PUT') or define('IS_PUT', \request()->isMethod('PUT'));

        // 定义是否DELETE请求
        defined('IS_DELETE') or define('IS_DELETE', \request()->isMethod('DELETE'));

        // 请求方式
        defined('REQUEST_METHOD') or define('REQUEST_METHOD', \request()->method());
    }

    private function initSystemConst()
    {
        // 项目根目录
        defined('ROOT_PATH') or define('ROOT_PATH', base_path());

        // 文件上传目录
        defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));

        // 图片上传目录
        defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));

        // 临时存放目录
        defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/temp");

        // 定义普通图片域名
        defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));

        // 数据表前缀
        defined('DB_PREFIX') or define('DB_PREFIX', DB::connection()->getTablePrefix());

        // 数据库名
        defined('DB_NAME') or define('DB_NAME', DB::connection()->getDatabaseName());

        // 系统全称
        define('SITE_NAME', env('SITE_NAME'));
        // 系统简称
        define('NICK_NAME', env('NICK_NAME'));
        // 系统版本号
        define('VERSION', env('VERSION'));
    }

}
